package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Coursemark;
import com.hs.sfzy.service.CoursemarkService;
import com.hs.sfzy.service.impl.CoursemarkServiceImpl;
import com.hs.sfzy.util.Result;
import com.hs.sfzy.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.Date;

/**
 * @ClassName CoursemarkController
 * @Description 课程评价控制器层
 * @Author shanglin
 * @Date 2024年10月24日 11:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/coursemark")
public class CoursemarkController {
    @Autowired
    private CoursemarkService coursemarkService;

    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "5") int pageSize,
            Coursemark coursemark){
        PageInfo<Coursemark> pageInfo = coursemarkService.selectByPage(pageNum, pageSize, coursemark);
        return Result.success(pageInfo);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") int id){
        int num = coursemarkService.deleteById(id);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @PostMapping
    public Object insert(@RequestBody Coursemark coursemark){
        coursemark.setCreateTime(new Date());
        int num = coursemarkService.insert(coursemark);
        if (num != 0){
            return Result.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    // 根据课程id查评价
    @GetMapping("/findByCourseId")
    public Result selectByCourseId(long courseId){
        List<Coursemark> coursemarks = coursemarkService.selectByCourseId(courseId);
        return Result.success(coursemarks);
    }
}
